package com.example.sls.dao.field;

import com.example.sls.pojo.field.Fields;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 场地表(Fields)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:54:53
 */
public interface FieldsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from fields where id=#{id}")
  Fields queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param fields   查询条件
   * @param pageSize 分页对象
   * @param offset   偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from fields where 1=1" +
          " and (id=#{fields.id} or (isnull(#{fields.id}) or #{fields.id}=0))" +
          " and (name=#{fields.name} or (isnull(#{fields.name}) or #{fields.name}=''))" +
          " and (location=#{fields.location} or (isnull(#{fields.location}) or #{fields.location}=''))" +
          " and (field_type=#{fields.fieldType} or (isnull(#{fields.fieldType}) or #{fields.fieldType}=''))" +
          " and (image_url=#{fields.imageUrl} or (isnull(#{fields.imageUrl}) or #{fields.imageUrl}=''))" +
          " and (rules=#{fields.rules} or (isnull(#{fields.rules}) or #{fields.rules}=''))" +
          " and (created_at=#{fields.createdAt} or (isnull(#{fields.createdAt}) or #{fields.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<Fields> queryAllByLimit(Fields fields, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from fields")
  long count();

  /**
   * 新增数据
   *
   * @param fields 实例对象
   * @return 影响行数
   */
  @Insert("insert into fields (id,name,location,field_type,image_url,rules,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{name}!='' and !isnull(#{name}),#{name},null)," +
          "if(#{location}!='' and !isnull(#{location}),#{location},null)," +
          "if(#{fieldType}!='' and !isnull(#{fieldType}),#{fieldType},null)," +
          "if(#{imageUrl}!='' and !isnull(#{imageUrl}),#{imageUrl},null)," +
          "if(#{rules}!='' and !isnull(#{rules}),#{rules},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(Fields fields);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param fields 实例对象
   * @return 影响行数
   */
  @Update("update fields set " +
          "name=if(isnull(#{name}) or #{name}='',name,#{name})," +
          "location=if(isnull(#{location}) or #{location}='',location,#{location})," +
          "field_type=if(isnull(#{fieldType}) or #{fieldType}='',field_type,#{fieldType})," +
          "image_url=if(isnull(#{imageUrl}) or #{imageUrl}='',image_url,#{imageUrl})," +
          "rules=if(isnull(#{rules}) or #{rules}='',rules,#{rules})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(Fields fields);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from fields where id=#{id}")
  int deleteById(Integer id);
}

